package filecopy;

import java.io.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class FileCopyDemo {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入所要复制的文件路径(输入0结束)");
            String s1 = scanner.next();
            if ("0".equals(s1)) {
                System.exit(0);
            }
            System.out.println("请输入所要复制文件到的路径(输入0结束)");
            String s2 = scanner.next();
            if ("0".equals(s2)) {
                System.exit(0);
            }
            boolean flag = fileCopy(s1, s2);
            if (flag) {
                System.out.println("复制完成");
            }
        }
    }

    public static boolean fileCopy(String s1, String s2) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(s1));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(s2));
        boolean flag = false;
        byte[] readBytes = new byte[1024];
        int read = 0;
        while ((read = bufferedInputStream.read(readBytes)) != -1) {
            bufferedOutputStream.write(readBytes, 0, read);
            flag = true;
        }
        return flag;
    }
}
